<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
	<script type="text/javascript">
	
		//定义操作类型
		var oper_type;
		
		//城市树数据源
		var tree_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["cityno","cityname","parentno","flag"],
    		url: "tree!city.action"
    	});
    	//城市列表数据源
    	var data_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields: ["cityno","cityname","parentno","flag","py"],
    		url: "city!list.action"
    	});
    	//定义数据显示面板
		var p1 = {
			id: "p1",xtype: "grid",
			stripeRows:true,
			loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
			autoExpandColumn:"cityname",//自动填充
			autoExpandMin : 100,//自动填充最小宽度
			store: data_store,
			headersDisabled: true,
			columns:[
	        	new Ext.grid.RowNumberer({header: "序号", width:35}),
    					{header: "区号",	dataIndex: "cityno", menuDisabled: true},
    					{header: "名称",	dataIndex: "cityname", id: "cityname", menuDisabled: true},
    					{header: "检索拼音",	dataIndex: "py", menuDisabled: true}
			],
			buttonAlign: "center",
			bbar: new Ext.ux.PagingToolbar({
				id:"cityPaging",
            	pageSize: limit,
            	store: data_store
        	}),
			buttons:[{text:"新增", handler: function(){
				var node = Ext.getCmp("cityTree").getSelectionModel().getSelectedNode();
				if(node) {
					if(node.isLeaf()) {
						node = node.parentNode;
					}
					oper_type = "add";
					Ext.getCmp("p2").resetParam(node.id);
					Ext.getCmp("mycontainer").layout.setActiveItem(1);
				} else {
					Ext.Msg.show({
		   				title:"警告",
		   				msg: "没有任何城市被选中，无法进行新增操作!",
		   				buttons: Ext.Msg.OK,
		   				animEl: "elId",
		   				icon:Ext.MessageBox.WARNING
						});
				}
			}},{text:"修改", handler: function(){
				var record = Ext.getCmp("p1").getSelectionModel().getSelected();
				if (record){
					Ext.getCmp("p2").setParam(record.data);
					oper_type = "upd";
					Ext.getCmp("mycontainer").layout.setActiveItem(1);
				}
				 else {
					Ext.Msg.show({
		   				title:"警告",
		   				msg: "没有任何行被选中，无法进行修改操作!",
		   				buttons: Ext.Msg.OK,
		   				animEl: "elId",
		   				icon:Ext.MessageBox.WARNING
						});
					}			
			}}
			/*
			,{text:"删除", handler: function(){
				var delPanel = this.ownerCt.ownerCt;
				var record = delPanel.getSelectionModel().getSelected();
				if(record){
					Ext.Msg.confirm("警告","删除城市将一并删除该城市对应的封装计费信息.<br/>确定要删除吗?",function(button) {
						if(button == "yes") {
							Ext.Ajax.request({
								params: {"city.cityno":record.data.cityno},
								url: "city!del.action",
								success: function(response, opts){
									var d=eval("("+response.responseText+")");
									if(d.code == "0"){
										var node = Ext.getCmp("cityTree").getSelectionModel().getSelectedNode();
										if(node) {
											if(node.isLeaf()) {
												node = node.parentNode;
											}
											node.reload();
											node.select();
										};
										Ext.getCmp("p1").getBottomToolbar().doRefresh();

										Ext.Msg.show({
											title:"提示",
		   									msg:"操作成功!",
		   									buttons:Ext.Msg.OK,
		   									icon: Ext.MessageBox.INFO
										});
									} else {
										Ext.Msg.show({
		   									title:"提示",
		   									msg:"操作失败!",
		  									buttons:Ext.Msg.OK,
		   									icon: Ext.MessageBox.ERROR
										});
									}
								}
							});
						}
					});
				} else {
					Ext.Msg.show({
		   				title:"警告",
		   				msg: "没有任何行被选中，无法进行删除操作!",
		   				buttons: Ext.Msg.OK,
		   				animEl: "elId",
		   				icon:Ext.MessageBox.WARNING
					});
				}
			}}
			*/]
		};
		
		var p2 = {
			id: "p2",
			xtype: "form",
		    bodyStyle:"padding:5px 5px 0",
			defaultType: "textfield",
			items: [{
				id: "city.cityno",   
				fieldLabel: "区号", 	
				allowBlank: false,
				vtype:"integer",
				name: "city.cityno",
				anchor: "100%",
				maxLength: 5
			},{
				id: "city.cityname", 
				fieldLabel: "名称",	
				allowBlank: false,
				name: "city.cityname",
				vtype:"alphanumchinese",
				anchor: "100%",
				validator:function (val){
	        		if(val=="")return "该输入项为必输项！";
	        		if(Ext.fucCheckCHLength(val)>10){
	        			return "请输入10个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
	        		}
	        		return true;
	        	}
			},{
				id: "city.py", 
				fieldLabel: "检索拼音",	
				allowBlank: false,
				name: "city.py",
				vtype:"alpha",
				anchor: "100%",
				validator:function (val){
	        		if(val=="")return "该输入项为必输项！";
	        		if(Ext.fucCheckCHLength(val)>30){
	        			return "请输入30个字符范围内的值!";
	        		}
	        		return true;
	        	}
			},{
				id: "city.parentno", 
				hidden:true, 			
				name: "city.parentno"
			},{
				id: "city.flag",
				fieldLabel: "所属城市",
				name: "city.flag",
				disabled: true,
				anchor: "100%"
			}],
			buttonAlign: "center",
			buttons:[{text:"保存", id:"save", handler: function(e){
				var form = this.ownerCt.ownerCt.getForm();
				if(!form.isValid()){
					Ext.Msg.show({
		   				title:"警告",
		   				msg: "验证不通过!",
		   				buttons: Ext.Msg.OK,
		   				animEl: "elId",
		   				icon:Ext.MessageBox.WARNING
					});
					return;
				}
				var parentno = Ext.getCmp("city.parentno").getValue();
				var cityno = Ext.getCmp("city.cityno").getValue();
				var cityname = Ext.getCmp("city.cityname").getValue();
				var py = Ext.getCmp("city.py").getValue();				
				e.setDisabled(true);
				if(oper_type == "add") {
					Ext.Ajax.request({
						clientValidation: true,
						params: {
							"city.parentno": parentno,
							"city.cityno": cityno,
							"city.cityname": cityname,
							"city.py": py,
							"city.flag": 0
						},
						url: "city!add.action",
						success: function(response, opts){
							var d=eval("("+response.responseText+")");
							if(d.code == "0"){
								var node = Ext.getCmp("cityTree").getSelectionModel().getSelectedNode();
								if(node) {
									if(node.isLeaf()) {
										node = node.parentNode;
									}
									node.reload();
									node.select();
								};
								Ext.getCmp("p1").getBottomToolbar().doRefresh();
								Ext.getCmp("cityPaging").doRefresh();//刷新页面
								Ext.Msg.show({
		   							title:"提示",
		   							msg:"操作成功!<br/>已添加该城市的默认封装计费信息.",
		   							buttons:Ext.Msg.OK,
		   							icon: Ext.MessageBox.INFO
								});
								
								form.ownerCt.layout.setActiveItem(0);
							} else {
								Ext.Msg.show({
		  							title:"提示",
		   							msg:"操作失败!",
		   							buttons:Ext.Msg.OK,
		   							icon: Ext.MessageBox.ERROR
								});
							}
							Ext.getCmp("save").setDisabled(false);
						}
					});
				} else if(oper_type == "upd") {
					Ext.Ajax.request({
						clientValidation: true,
						params: {
							"city.parentno": parentno,
							"city.cityno": cityno,
							"city.cityname": cityname,
							"city.py": py,
							"city.flag": 0
						},
						url: "city!upd.action",
						success: function(response, opts){
							var d=eval("("+response.responseText+")");
							if(d.code == "0"){
								var node = Ext.getCmp("cityTree").getSelectionModel().getSelectedNode();
								if(node) {
									if(node.isLeaf()) {
										node = node.parentNode;
									}
									node.reload();
									node.select();
								};
								Ext.getCmp("p1").getBottomToolbar().doRefresh();

								Ext.Msg.show({
		   							title:"提示",
		   							msg:"操作成功!",
		   							buttons:Ext.Msg.OK,
		   							icon: Ext.MessageBox.INFO
									});
								form.ownerCt.layout.setActiveItem(0);
							} else {
								Ext.Msg.show({
		   							title:"提示",
		   							msg:"操作失败!",
		   							buttons:Ext.Msg.OK,
		   							icon: Ext.MessageBox.ERROR
								});
							}
						Ext.getCmp("save").setDisabled(false);
						}
					});
				}
				}},{text:"取消", handler: function(){
						this.ownerCt.ownerCt.ownerCt.layout.setActiveItem(0);
					}
				}],
			setParam: function(data){
				Ext.getCmp("city.cityno").setValue(data.cityno);
				Ext.getCmp("city.cityname").setValue(data.cityname);
				Ext.getCmp("city.parentno").setValue(data.parentno);
				Ext.getCmp("city.py").setValue(data.py);
				Ext.getCmp("city.cityno").setDisabled(true);
			},
			resetParam: function(parentno){
				Ext.getCmp("city.cityno").setValue("");
				Ext.getCmp("city.cityname").setValue("");
				Ext.getCmp("city.parentno").setValue(parentno);
				Ext.getCmp("city.py").setValue("");
				Ext.getCmp("city.cityno").setDisabled(false);
			}
		};


		var city_pel =new Ext.Panel({
			border : false, 
			layout: "border",
			items: [{
				id: "cityTree",
				xtype: "billTreePanel",
				dataUrl: "tree!city.action",
				autoScroll: true,
				root: new Ext.tree.AsyncTreeNode({
					id: "root",
					expanded : true		// 自动展开节点
				}),
				listeners: {
					"click": function(node){
						tree_store.load({params:{node:node.attributes.id}});
						data_store.baseParams = data_store.baseParams || {};
						if(node.isLeaf()) {
							if(node.parentNode.attributes.id != "root") {
								Ext.getCmp("city.flag").setValue(node.parentNode.attributes.text);
							} else {
								Ext.getCmp("city.flag").setValue("直辖市");
							}
							data_store.baseParams["city.cityno"] = node.attributes.id;
							delete data_store.baseParams["city.parentno"];
						} else {
							Ext.getCmp("city.flag").setValue(node.text);
							data_store.baseParams["city.parentno"] = node.attributes.id;
							delete data_store.baseParams["city.cityno"];
						}
						
						data_store.load({params:{start : 0,limit : limit}});
						Ext.getCmp("mycontainer").layout.setActiveItem(0);
					}
				}
			}, {
				id: "mycontainer", 
				region: "center",
				border : false, 
				layout: "card",
				activeItem:0,
				items: [p1, p2]
		}]
		});	

	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(city_pel);
	CenterPanel.doLayout();
	</script>
